
package com.gp.activity.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.gp.R;
import com.gp.activity.base.BaseActivity;
import com.gp.activity.detail.CityGuideDetailActivity;
import com.gp.data.CityGuides;

/**
 * @author shijunxing
 * @className CityGuideActivity
 * @date July 4, 2013 5:09:17 PM
 * @version 1.0
 * @Description 城市指南类
 */
public class CityGuideActivity extends BaseActivity {

    private ListView mCityguideListView;
    // 返回按钮
    private Button mButton;

    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cityguide);
        intiView();
        bindData();
        initListener();
    }

    /**
     * @Title: intiView
     * @param
     * @return void
     * @throws
     * @date Jul 18, 2013 2:59:45 PM
     * @version 1.0
     * @Description: 初始化控件
     */
    private void intiView() {

        mCityguideListView = (ListView) findViewById(R.id.lv_cityguide);
        mButton = (Button) findViewById(R.id.btn_cityguide_back);
    }

    /**
     * @Title: bindData
     * @param
     * @return void
     * @throws
     * @date Jul 18, 2013 3:01:23 PM
     * @version 1.0
     * @Description: 为控件绑定数据
     */
    private void bindData() {

        mCityguideListView.setAdapter(new SimpleAdapter(this, getData(),
                R.layout.citiyguide_list_item, new String[] { "btitle",
                        "stitle" }, new int[] { R.id.tv_cityguidelist_titlebig,
                        R.id.tv_cityguidelist_titlesmall }));
    }

    /**
     * @Title: getData
     * @param @return
     * @return List<HashMap<String,String>>
     * @throws
     * @date Jul 18, 2013 3:02:16 PM
     * @version 1.0
     * @Description: listview的数据源
     */
    private List<HashMap<String, String>> getData() {

        // 显示的数据
        String[] qBtitle = { "桂林介绍", "风土人情", "饮食文化", "缤纷节日", "市内交通", "离开交通",
                "娱乐", "特产" };

        // 英文
        String[] qStitle = { "About guiliin", "Custom and practices",
                "Catering culture", "Festival", "Transportation",
                "Leave guilin", "Entertainment", "Sppecialties" };

        List<HashMap<String, String>> qList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> qMap;

        for (int j = 0; j < qBtitle.length; j++) {

            qMap = new HashMap<String, String>();
            qMap.put("btitle", qBtitle[j]);
            qMap.put("stitle", qStitle[j]);
            qList.add(qMap);
        }

        return qList;

    }

    
    /**
     * @Title: readFileFromRaw  
     * @param @param pResID
     * @param @return   
     * @return String   
     * @throws 
     * @date Sep 3, 2013 1:07:23 AM
     * @version 1.0
     * @Description: 从res/raw/文件夹读出的数据，为景点的介绍
     */
    private String readFileFromRaw(int pResID) {

        // 资源句柄
        Resources qResources = this.getResources();
        // 资源输入流
        InputStream qInputStream = null;
        // String缓冲器，缓存从res/raw/文件夹读出的数据
        StringBuffer qStringBuffer = new StringBuffer();
        // 用于读取缓存器中的数据
        BufferedReader qBufferedReader = null;
        String qString;
        //
        qStringBuffer.append("");

        try {
            // 从res/raw/文件夹独处数据
            qInputStream = qResources.openRawResource(pResID);
            // 获取数据，编码格式UTF-8，支持中文
            qBufferedReader = new BufferedReader(new InputStreamReader(
                    qInputStream, "UTF-8"));
            while ((qString = qBufferedReader.readLine()) != null) {

                qStringBuffer.append(qString);
                qStringBuffer.append("\n");
            }
        }
        catch (NotFoundException e) {
            Toast.makeText(this, "文本文件不存在", 100).show();
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            Toast.makeText(this, "文本编码出现异常", 100).show();
            e.printStackTrace();
        }
        catch (IOException e) {
            Toast.makeText(this, "文件读取错误", 100).show();
            e.printStackTrace();
        }
        finally {
            try {
                if (qInputStream != null) {
                    qInputStream.close();
                }
                if (qBufferedReader != null) {
                    qBufferedReader.close();
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return qStringBuffer.toString();
    }

    /**
     * @Title: initListener
     * @param
     * @return void
     * @throws
     * @date Jul 18, 2013 3:04:04 PM
     * @version 1.0
     * @Description: 初始化控件监听器
     */
    private void initListener() {

        // 返回按钮的监听
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CityGuideActivity.this.finish();
            }
        });

        // 列表项点击监听
        mCityguideListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {

                Intent qIntent = new Intent();

                qIntent.setClass(CityGuideActivity.this,
                        CityGuideDetailActivity.class);
                // 将数据根据点击位置传到CityGuideDetailActivity，这里的数据放在CityGuides这个类里面
                qIntent.putExtra("title",
                        CityGuides.INFORMATION.get(position).title);
                qIntent.putExtra("imaID",
                        CityGuides.INFORMATION.get(position).imaID);
                qIntent.putExtra("descriptions",
                        readFileFromRaw(CityGuides.INFORMATION.get(position).descriptions));

                CityGuideActivity.this.startActivity(qIntent);
            }
        });

    }
}
